//
// Created by root on 6/22/17.
//

#include "toolbar.h"

#define TOOL_ITEM(_stock,_ti,_tooltip) _ti = gtk_tool_button_new_from_stock(GTK_STOCK_##_stock); \
    gtk_tool_item_set_homogeneous(_ti, FALSE); \
    gtk_toolbar_insert(GTK_TOOLBAR(*toolbar), _ti, -1); \
    gtk_widget_set_tooltip_text(GTK_WIDGET(_ti), _tooltip)

#define TOOL_ITEM_INSENSITIVE(_stock,_ti,_tooltip) _ti = gtk_tool_button_new_from_stock(GTK_STOCK_##_stock); \
    gtk_tool_item_set_homogeneous(_ti, FALSE); \
    gtk_widget_set_sensitive(GTK_WIDGET(_ti), FALSE); \
    gtk_toolbar_insert(GTK_TOOLBAR(*toolbar), _ti, -1); \
    gtk_widget_set_tooltip_text(GTK_WIDGET(_ti), _tooltip)

#define TOOL_ITEM_WITH_LABEL(_stock,_ti,_tooltip,_label) _ti = gtk_tool_button_new_from_stock(GTK_STOCK_##_stock); \
    gtk_tool_item_set_homogeneous(_ti, FALSE); \
    gtk_toolbar_insert(GTK_TOOLBAR(*toolbar), _ti, -1); \
    gtk_tool_button_set_label(GTK_TOOL_BUTTON(_ti), _label); \
    gtk_widget_set_tooltip_text(GTK_WIDGET(_ti), _tooltip)

#define TOOL_ITEM_SEPARATOR()   gtk_toolbar_insert(GTK_TOOLBAR(*toolbar), gtk_separator_tool_item_new(), -1)

#define CLICKED(_ti,_callback)     g_signal_connect (G_OBJECT(_ti), "clicked", G_CALLBACK(_callback), app)

void create_toolbar(GtkWidget **toolbar, GtkAccelGroup *mainaccel, FunnyWindow *app) {
    GtkToolItem *new;
    GtkToolItem *open;
    GtkToolItem *save;
    GtkToolItem *saveas;
    GtkToolItem *exit;
    GtkToolItem *copy;
    GtkToolItem *cut;
    GtkToolItem *paste;
    GtkToolItem *undo;
    GtkToolItem *redo;
    GtkToolItem *find;
    GtkToolItem *replace;
    GtkToolItem *gotoln;
    GtkToolItem *indent;
    GtkToolItem *unindent;
    GtkToolItem *preferences;

    *toolbar = gtk_toolbar_new();
    gtk_container_set_border_width(GTK_CONTAINER(*toolbar), 2);
    gtk_toolbar_set_show_arrow(GTK_TOOLBAR(*toolbar), TRUE);
    gtk_toolbar_set_style(GTK_TOOLBAR(*toolbar), GTK_TOOLBAR_BOTH);

    TOOL_ITEM(NEW, new, "New file ");
    TOOL_ITEM(OPEN, open, "Open existing file ");
    TOOL_ITEM(SAVE, save, "Save file ");
    TOOL_ITEM(SAVE_AS, saveas, "Save as new file ");
    TOOL_ITEM_SEPARATOR();
    TOOL_ITEM(QUIT, exit, "Quit ");
    TOOL_ITEM_SEPARATOR();
    TOOL_ITEM_INSENSITIVE(UNDO,undo,"Undo previous edits ");
    TOOL_ITEM_INSENSITIVE(REDO,redo,"Redo previous edits ");
    TOOL_ITEM_SEPARATOR();
    TOOL_ITEM(CUT, cut, "Cut selected text to clipboard ");
    TOOL_ITEM(COPY, copy, "Copy selected text to clipboard ");
    TOOL_ITEM(PASTE, paste, "Paste text from clipboard ");
    TOOL_ITEM_SEPARATOR();
    TOOL_ITEM(FIND, find, "Find text ");
    TOOL_ITEM_WITH_LABEL(FIND_AND_REPLACE,replace,"Find and replace text ","Replace");
    TOOL_ITEM_WITH_LABEL(INDEX,gotoln,"Goto line in text ","Goto");
    TOOL_ITEM_SEPARATOR();
    TOOL_ITEM_WITH_LABEL(INDENT,indent,"Increase indent ","Indent");
    TOOL_ITEM_WITH_LABEL(UNINDENT,unindent,"Decrease indent ","Unindent");
    TOOL_ITEM_SEPARATOR();
    TOOL_ITEM_WITH_LABEL(PREFERENCES,preferences,"Preferences ","Settings");

    /*
     * Toolbar callbacks - simply connect to menu functions
     */
    CLICKED(new,         menu_file_new_activate);
    CLICKED(open,        menu_file_open_activate);
    CLICKED(save,        menu_file_save_activate);
    CLICKED(saveas,      menu_file_saveas_activate);
    CLICKED(exit,        menu_file_quit_activate);
    CLICKED(undo,        menu_edit_undo_activate);
    CLICKED(redo,        menu_edit_redo_activate);
    CLICKED(cut,         menu_edit_cut_activate);
    CLICKED(copy,        menu_edit_copy_activate);
    CLICKED(paste,       menu_edit_paste_activate);
    CLICKED(find,        menu_edit_find_activate);
    CLICKED(replace,     menu_edit_replace_activate);
    CLICKED(gotoln,      menu_edit_goto_activate);
    CLICKED(indent,      menu_tools_indent_activate);
    CLICKED(unindent,    menu_tools_unindent_activate);
    CLICKED(preferences, menu_edit_preferences_activate);
}
